잠시만 기다려 주세요

     '대통령을 욕하는 것은 민주사회에서 주권을 가진 시민의 당연한 권리입니다. 대통령을 욕하는 것으로 주권자가 스트레스를 해소할 수 있다면 저는 기쁜 마음으로 들을 수 있습니다. - 노무현 -'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (19)  |  여러가지 팁 (1095)  |  추천 및 재미 (163)  |  자료실 (28)  |  
시사, 이슈, 칼럼, 평론, 비평 (795)  |  끄적거림 (142)  |  문예 창작 (719)  |  바람 따라 (75)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    postgresql

postgresql - PostgreSQL의 트리거(TRIGGER)와 함수(function) 예제
이 름 : 바다아이   |   조회수 : 10707         짧은 주소 : https://www.bada-ie.com/su/?591591789061

PostgreSQL의 트리거와 펑션 예제를 만들어 봤다.

자료를 찾는데 너무 힘들어서 나중에 잊기전에 정리해봤다.

 

 

 

● 우선 테스트용 테이블 작성 

 

CREATE TABLE datalog(

    logtime timestamp PRIMARY KEY,

    data varchar,

    content varchar

);

 

* 테이블 지우기

DROP TABLE datalog;

 

● 펑션(Function) 함수 예제

    이 함수가 실행되면 지정 테이블의 레코드가 10개를 넘으면 가장 오래된 것부터 지운다.

 

CREATE FUNCTION data_fun() RETURNS trigger AS $data_fun$

    DECLARE

rowcount integer;

        delcount integer;

        maxcount integer;

    BEGIN

        maxcount := 10;

        SELECT count(*) logtime into rowcount  FROM datalog;

        delcount := rowcount - maxcount + 1;

 

        IF delcount > 0 THEN

   DELETE FROM datalog WHERE logtime IN (SELECT logtime FROM datalog ORDER BY logtime LIMIT delcount);

        END IF;

 

RETURN NEW;

    END;

$data_fun$ LANGUAGE plpgsql;

 

* 펑션 지우기

DROP FUNCTION data_fun();

 

● 트리거(Trigger) 함수 예제

    지정 Table에서 추가나 변경이 이루어질 때 지정 펑션이 실행된다.

 

CREATE TRIGGER data_trg BEFORE INSERT OR UPDATE ON datalog

    FOR EACH ROW EXECUTE PROCEDURE data_fun();

 

* 트리거 지우기

DROP TRIGGER data_trg ON datalog;

 

 

● 테스트용 데이터

 

insert into datalog(logtime, data) values('2015-01-01 12:12:01', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:02', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:03', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:04', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:05', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:06', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:07', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:08', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:09', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:01', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:02', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:03', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:04', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:05', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:06', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:07', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:08', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:09', 'aa');

 
위의 데이터를 입력하면 로그는 10개 행만 테이블에 저장된다.
데이터를 추가하기 전에 트리거의 연결된 펑션이 실행되어
테이블의 행이 10개를 넘어가면 가장 옛날 데이터를 삭제한다.


출처 : http://wwwi.tistory.com/348
 
| |





      1 page / 2 page
번 호 카테고리 제 목 이름 조회수
45 postgresql postgresql ... postgresql 14 .. postgresql.conf port 5432 .. 바다아이 1817
44 postgresql , count(*) .... 바다아이 8487
43 postgresql How to do an update + join in PostgreSQL?, 바다아이 7762
42 postgresql sequence(퀀) 바다아이 10233
41 postgresql , , , index create, , 바다아이 10223
40 postgresql postgresql log_timezone .... 바다아이 8271
39 postgresql postgresql SEQUENCE reset .... 바다아이 10192
38 postgresql [PostgreSql] WITH , , Operator 바다아이 9403
37 postgresql postgresql for, foreach , 바다아이 11185
36 postgresql postgresql , , into ... 바다아이 11319
35 postgresql postgresql PL/pgSQL - SQL Procedural Language, , 바다아이 12904
34 postgresql postgresql ... .. , , 바다아이 14432
33 postgresql postgresql CSV export/import 바다아이 10561
32 postgresql postgresql tablespace , .... 바다아이 15780
31 postgresql postgresql 10 partitioning, ... , ... 바다아이 11100
30 postgresql Using PostgreSQL Arrays, ... ... 바다아이 11145
현재글 postgresql PostgreSQL (TRIGGER) (function) 바다아이 10708
28 postgresql Optimize and Improve PostgreSQL Performance with VACUUM, ANALYZE, and REINDEX 바다아이 11322
27 postgresql postgresql tuple . vacuumdb .. , . 바다아이 10909
26 postgresql postgresql , .. 바다아이 11107
25 postgresql postgresql , size, 바다아이 13045
24 postgresql postgresql , , .... 바다아이 9888
23 postgresql PostgreSQL Replication, , , master, slave 바다아이 13465
22 postgresql postgresql case 바다아이 9943
21 postgresql postgresql with 바다아이 10391
20 postgresql postgresql , , string 바다아이 14334
19 postgresql Postgresql partitioning table , , , 바다아이 10759
18 postgresql PostgreSQL 바다아이 12385
17 postgresql postgresql vacuumdb, psql, pg_dump password crontab , pgpass 바다아이 12370
16 postgresql postgresql sequence 퀀 auto_increment . 바다아이 11322
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.21784
to webmaster... gogo sea. gogo sea.